﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.CarType.Views;
using CarAppDto.CarType.BrandDtos;
using CarAppDto.CarType.CarDtos;
using D.UtilCore.Dtos;
using CarAppDto.CarType.AreaDtos;
using CarAppClient.Views.CarType.Views.Brand;
using CarAppClient.Views.CarType.ViewModels.CarType;
using CarAppClient.Views.Country;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.Common;

namespace CarAppClient.Views.CarType.ViewModels.Brand
{
    public class BrandModifyViewModel : BaseModifyViewModel<Page_Brand_Modify>
    {      
        #region 加载页面数据

        public BrandModifyViewModel(Page_Brand_Modify owner, string? id, ViewStatus? viewStatus) : base(owner, id, viewStatus)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected async override void LoadData()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<RepBrand>.GetAsync($"{ApiUrl.GetBrand}/{Id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    MsgUtil.Error(res.ErrorMsg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    MsgUtil.Error("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {                      
                        Name = res.Data.Name;
                        Code = res.Data.Code;                      
                        EnglishName = res.Data.EnglishName;

                        var resArea = await HttpHelper<RepArea>.GetAsync($"{ApiUrl.GetArea}/{res.Data.BelongingPlace}");
                        repArea = resArea.Data;
                        Area = repArea?.Name;

                        var resCountry = await HttpHelper<RepCountry>.GetAsync($"{ApiUrl.GetCountry}/{res.Data.ManufacturingCountry}");
                        repCountry = resCountry.Data;
                        Country = repCountry?.Name;
                    }
                }
            }
        }
        #endregion

        #region 新增、修改 参数

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }
        RepArea repArea = null;
        private string? area;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Area
        {
            get { return area; }
            set { area = value; RaisePropertyChanged(); }
        }

        private string? code;
        /// <summary>
        /// 编号
        /// </summary>
        public string? Code
        {
            get { return code; }
            set { code = value; RaisePropertyChanged(); }
        }

        private string? englishName;
        /// <summary>
        /// 描述
        /// </summary>
        public string? EnglishName
        {
            get { return englishName; }
            set { englishName = value; RaisePropertyChanged(); }
        }

        RepCountry repCountry = null;
        private string? country;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Country
        {
            get { return country; }
            set { country = value; RaisePropertyChanged(); }
        }
    
        #endregion

        #region 保存事件
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="vm"></param>
        protected override async void Submit(object? obj)
        {
            // 基础数据判断
            if (string.IsNullOrEmpty(Name))
            {
                MsgUtil.Warning("区域名称不可为空"); return;
            }

            // 基础数据判断
            if (string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddBrand, new ReqAddBrand()
                {
                    BelongingPlace = repArea?.Id,
                    ManufacturingCountry = repCountry?.Id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName
                }); ;

                //  判断结果
                MsgUtil.ShowMsg(res.Code, "新增成功", res.ErrorMsg);
            }
            else
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateBrand}", new ReqUpdateBrand()
                {
                    Id = Id,
                    Name = Name,
                    Code = Code,
                    EnglishName = EnglishName,
                    BelongingPlace = repArea?.Id,
                    ManufacturingCountry = repCountry?.Id
                });
                // 判断结果
                MsgUtil.ShowMsg(res.Code, "修改成功", res.ErrorMsg);
            }
            this.FromClose();
        }
        #endregion

       
        protected override async void Query(string? type)
        {
            if (type == "Area")
            {
                Page_Area_Query area_Query = new Page_Area_Query();
                ((AreaQueryViewModel)area_Query.DataContext).InformationRequested += Area_InformationRequested;
                area_Query.ShowDialog();
            }
            else if (type == "Country")
            {
                Page_Country_Query country_Query = new Page_Country_Query(areaid:repArea.Id);
                ((CountryQueryViewModel)country_Query.DataContext).InformationRequested += Country_InformationRequested;
                country_Query.ShowDialog();
            }
          
           
        }

        private void Area_InformationRequested(object sender, InformationEventArgs<RepArea> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repArea = e.Information;
            if (repArea?.Name != Area)
            {
                repCountry = null;
                Country = "";
            }
                Area = repArea?.Name;
            
        }

        private void Country_InformationRequested(object sender, InformationEventArgs<RepCountry> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repCountry = e.Information;
            Country = repCountry?.Name;
        }
    }
}
